﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Tunnel.Tools.FcmsTools
{
    /// <summary>
    /// 可变情报板的实用工具
    /// </summary>
    public static class FcmsUtilities
    {
        /// <summary>
        /// 计算带有\n分割的字符串的行数，如果最后一行为空，则改行不计入总行数
        /// </summary>
        /// <param name="text">要被计算的文本</param>
        /// <returns></returns>
        public static int GetLinesCount(string text)
        {
            string[] tempArray = Regex.Split(text, @"\\n");
            int count = tempArray.Length;
            if (tempArray[count - 1] == string.Empty)
            {
                count--;
            }
            return count;
        }

        /// <summary>
        /// 计算带有\n分割的字符串最大一行的字节数
        /// </summary>
        /// <param name="text">要被计算的文本</param>
        /// <returns></returns>
        public static int MaxLineLength(string text)
        {
            string[] tempArray = Regex.Split(text, @"\\n"); ;
            
            int maxLineLength = 0;
            foreach (var temp in tempArray)
            {
                if (GetLength(temp) >= maxLineLength)
                {
                    maxLineLength = GetLength(temp);
                }
            }
            return maxLineLength;
        }
          

        /// <summary>
        /// 获得某个字符串的字节长度
        /// </summary>
        /// <param name="str">用以被计算的字符串</param>
        /// <returns></returns>
        public static int GetLength(string str)
        {
            int length = 0;
            if (str.Length == 0)
            {
                return length;
            }
            byte[] tempArray = Encoding.ASCII.GetBytes(str);
            foreach (var temp in tempArray)
            {
                if (temp == 0x3f)
                {
                    length += 2;
                }
                else
                {
                    length++;
                }
            }
            return length;
        }
        /// <summary>
        /// 判断是否为全角字
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static bool IsDBC(char c)
        {
            return c > 255;
        }        

        public static int GetFontSizeNum(string fontSizeString)
        {
            return Convert.ToInt32(fontSizeString.Substring(0, 2));
        }
        #region 废弃代码
        /// <summary>
        /// 得到一个屏幕内可以容纳的半角字符总数
        /// </summary>
        /// <param name="fontSize">字体大小</param>
        /// <param name="screenWidth">屏幕宽度</param>
        /// <param name="screenHeight">屏幕高度</param>
        /// <returns>可容纳的半角字符总数</returns>
        public static int GetMaxSbcTextCountInScreen(int fontSize, int screenWidth, int screenHeight)
        {
            int maxInOneLine = 2 * (screenWidth / fontSize);
            int maxLines = screenHeight / fontSize;
            return maxInOneLine * maxLines;
        }
        
        /// <summary>
        /// 按照屏幕宽度和字体大小自动为文本增加换行
        /// </summary>
        /// <param name="text">情报板显示文本</param>
        /// <param name="fontSize">字体大小</param>
        /// <param name="screenWidth">屏幕宽度</param>
        /// <param name="screenHeight">屏幕高度</param>
        /// <returns></returns>
        public static string[] InsertEnterIntoString(string text, int fontSize, int screenWidth, int screenHeight)
        {
            string enterString = @"\n";
            string textWithoutEnter = text.Replace("\r\n", string.Empty);
            bool needInsertEnterFlag = true;
            StringBuilder resultStringBuilder = new StringBuilder();

            while (needInsertEnterFlag)
            {
                needInsertEnterFlag = false;
                int count = 0;
                int index = 0;
                foreach (var textChar in textWithoutEnter)
                {
                    if (!IsDBC(textChar))
                    {
                        count += 1;
                        index += 1;
                    }
                    else
                    {
                        count += 2;
                        index += 1;
                    }
                    if (count <= ((screenWidth * 2) / fontSize) - 1)
                    {
                        resultStringBuilder.Append(textChar);
                        needInsertEnterFlag = false;
                    }
                    else
                    {

                        if (count == (screenWidth * 2) / fontSize)
                        {
                            string nextLineText = textWithoutEnter.Substring(index);
                            if (!string.IsNullOrEmpty(nextLineText))
                            {
                                resultStringBuilder.Append(textChar);
                                resultStringBuilder.Append(enterString);
                                textWithoutEnter = nextLineText;
                                needInsertEnterFlag = true;
                                break;
                            }
                            else
                            {
                                resultStringBuilder.Append(textChar);
                                needInsertEnterFlag = false;
                                break;
                            }
                        }
                        else
                        {
                            resultStringBuilder.Append(enterString);
                            textWithoutEnter = textWithoutEnter.Substring(index - 1);
                            needInsertEnterFlag = true;
                            break;
                        }
                    }
                }
            }
            return Regex.Split(resultStringBuilder.ToString(), @"\\n");

        }
        #endregion

        /// <summary>
        /// 文本按照屏幕所能容纳每一行的字符数进行分行
        /// </summary>
        /// <param name="text">情报板显示文本</param>
        /// <param name="fontSize">字体大小</param>
        /// <param name="screenWidth">屏幕宽度</param>
        /// <param name="screenHeight">屏幕高度</param>
        /// <returns>每一行字符串组成的LIST</returns>
        public static List<string> GetTextLines(string text, int fontSize, int screenWidth)
        {
            List<string> stringList = new List<string>();
            
            bool newLineFlag = true;
            StringBuilder resultStringBuilder = new StringBuilder();

            while (newLineFlag)
            {
                newLineFlag = false;
                int count = 0;
                int index = 0;
                foreach (var textChar in text)
                {
                    if (!IsDBC(textChar))
                    {
                        count += 1;
                        index += 1;
                    }
                    else
                    {
                        count += 2;
                        index += 1;
                    }
                    if (count <= ((screenWidth * 2) / fontSize)-1)
                    {
                        resultStringBuilder.Append(textChar);
                        newLineFlag = false;
                    }
                    else
                    {
                        if (count == (screenWidth * 2) / fontSize)
                        {
                            string nextLineText = text.Substring(index);
                            if (!string.IsNullOrEmpty(nextLineText))
                            {
                                resultStringBuilder.Append(textChar);
                                stringList.Add(resultStringBuilder.ToString());
                                resultStringBuilder.Clear();
                                text = nextLineText;
                                newLineFlag = true;
                                break;
                            }
                            else
                            {
                                resultStringBuilder.Append(textChar);
                                stringList.Add(resultStringBuilder.ToString());
                                resultStringBuilder.Clear();
                                newLineFlag = false;
                                break;
                            }
                        }
                        else
                        {
                            stringList.Add(resultStringBuilder.ToString());
                            resultStringBuilder.Clear();
                            text = text.Substring(index - 1);
                            newLineFlag = true;
                            break;
                        }
                    }
                }
                if (count <= ((screenWidth * 2) / fontSize) - 1)
                {
                    stringList.Add(resultStringBuilder.ToString());
                }
            }
            return stringList;
        }
        #region 废弃代码
        /// <summary>
        /// 将信息按照屏幕大小进行分割，将多余的部分保存在nexScreenText中
        /// </summary>
        /// <param name="text">将要分割的信息</param>
        /// <param name="nextScreenText">多余的部分保存在nextScreenText中</param>
        /// <param name="fontSize">字体大小</param>
        /// <param name="screenWidth">屏幕宽度</param>
        /// <param name="screenHeight">屏幕高度</param>
        public static void AutoSplitScreen3(ref string text, out string nextScreenText, int fontSize, int screenWidth, int screenHeight)
        {
            int max = FcmsUtilities.GetMaxSbcTextCountInScreen(fontSize, screenWidth, screenHeight);
            string noEnterString = text.Replace(@"\n", string.Empty);
            string text1 = string.Empty;
            string text2 = string.Empty;
            if (!string.IsNullOrEmpty(noEnterString) && Encoding.Default.GetByteCount(noEnterString) > max)
            {

                string lastChar = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), 0, max).Substring(Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), 0, max).Length - 1, 1);
                if (lastChar != "?")
                {
                    text1 = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), 0, max);
                    text2 = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), max, Encoding.Default.GetByteCount(noEnterString) - max);

                }
                else //如果是奇数个，则需要往前提前半个全角字符截取
                {
                    text1 = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), 0, max - 1);
                    text2 = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), max - 1, Encoding.Default.GetByteCount(noEnterString) - max + 1);
                }

            }
            else
            {
                text1 = noEnterString;
                nextScreenText = string.Empty;
            }
            text = text1;
            nextScreenText = text2;

        }
        #endregion
        /// <summary>
        /// 将信息按照屏幕大小进行分割，将多余的部分保存在nextScreenText中
        /// </summary>
        /// <param name="text">将要分割的信息</param>
        /// <param name="nextScreen">多余的部分保存在nextScreen</param>
        /// <param name="fontSize">字体大小</param>
        /// <param name="screenWidth">屏幕宽度</param>
        /// <param name="screenHeight">屏幕高度</param>
        public static void AutoSplitScreen(ref string text, out string nextScreen, int fontSize, int screenWidth, int screenHeight)
        {
            string noEnterString = text.Replace(@"\n", string.Empty);
            List<string> textLines = GetTextLines(noEnterString, fontSize, screenWidth);
            int maxLines = screenHeight / fontSize;
            if (textLines.Count < maxLines)
            {
                maxLines = textLines.Count;
            }
            StringBuilder thisScreenBuilder = new StringBuilder();
            StringBuilder nextScreenBulilder = new StringBuilder();
            nextScreen = string.Empty;
            if (textLines.Count > 0)
            {
                for (int i = 0; i < maxLines - 1; i++)
                {
                    thisScreenBuilder.Append(textLines[i]);
                    thisScreenBuilder.Append(@"\n");

                }                
                thisScreenBuilder.Append(textLines[maxLines - 1]);
                text = thisScreenBuilder.ToString();//输出分屏并添加回车后的一屏
                for (int i = maxLines; i < textLines.Count; i++)
                {
                    nextScreenBulilder.Append(textLines[i]);                    
                }
                nextScreen = nextScreenBulilder.ToString();
            }

        }
    }
}
